<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='global-property-S-'>/**
</span> * @fileOverview 编辑器扩展类，引入这个扩展，控件可以支持编辑器功能。
 * @ignore
 */

var $ = require(&#39;jquery&#39;);

function initEditor (self) {
 var _self = self,
    controlCfgField = _self.get(&#39;controlCfgField&#39;),
    control = _self.get(controlCfgField),
    c = _self.addChild(control);
  _self.setInternal(controlCfgField,c);
}

<span id='BUI-Editor-Mixin'>/**
</span> * @class BUI.Editor.Mixin
 * 编辑器扩展类
 */
var Mixin = function () {
  initEditor(this);
};

Mixin.ATTRS = {
<span id='BUI-Editor-Mixin-property-acceptEvent'>  /**
</span>   * 接受更改的事件
   * @protected
   * @type {String}
   */
  acceptEvent : {
    value : &#39;autohide&#39;
  },
<span id='BUI-Editor-Mixin-property-preventHide'>  /**
</span>   * 当发生错误时是否阻止编辑器消失
   * @type {Boolean}
   */
  preventHide : {
    value : true
  },
<span id='BUI-Editor-Mixin-property-changeSourceEvent'>  /**
</span>   * 重置数据时的事件
   * @type {String}
   */
  changeSourceEvent : {
    value : &#39;show triggerchange&#39;
  },
<span id='BUI-Editor-Mixin-property-ignoreInputFields'>  /**
</span>   * 是否忽略掉输入框之类的键盘事件
   * @protected
   * @type {Boolean}
   */
  ignoreInputFields: {
    value :false
  },
<span id='BUI-Editor-Mixin-property-innerValueField'>  /**
</span>   * 内部控件的代表Value的字段
   * @protected
   * @type {String}
   */
  innerValueField : {

  },
<span id='BUI-Editor-Mixin-property-emptyValue'>  /**
</span>   * 空值的数据，清空编辑器时使用
   * @protected
   * @type {*}
   */
  emptyValue : {

  },
<span id='BUI-Editor-Mixin-property-controlCfgField'>  /**
</span>   * 内部控件配置项的字段
   * @protected
   * @type {String}
   */
  controlCfgField : {

  },
  focusable : {
    value : true
  },
  autoUpdate : {
    value : true
  },
  events : {
    value : {
<span id='BUI-Editor-Mixin-event-accept'>      /**
</span>       * @event
       * 接受更改
       */
      accept : false,
<span id='BUI-Editor-Mixin-event-cancel'>      /**
</span>       * @event
       * 取消更改
       */
      cancel : false
    }
  }
};

Mixin.prototype = {
  //绑定事件
  __bindUI : function(){
    var _self = this,
    acceptEvent = _self.get(&#39;acceptEvent&#39;),
    changeSourceEvent = _self.get(&#39;changeSourceEvent&#39;);

    if(acceptEvent){
      _self.on(acceptEvent,function(){
        if(_self.accept()){
          return ;
        }else if(_self.get(&#39;preventHide&#39;)){
          return false;
        }else{
          _self.cancel();
        }
      });
    }
    if(changeSourceEvent){
      _self.on(changeSourceEvent,function(){
        _self.setValue(_self.getSourceValue());
        if(_self.get(&#39;visible&#39;)){
          _self.focus();
        }
      });
    }
  },
<span id='BUI-Editor-Mixin-method-getInnerControl'>  /**
</span>   * @protected
   * 获取编辑器的内部控件
   * @return {BUI.Component.Controller} 用于编辑数据的内部数据
   */
  getInnerControl : function(){
    var _self = this,
      children = _self.get(&#39;children&#39;);
    return children[0];
  },
<span id='BUI-Editor-Mixin-method-setValue'>  /**
</span>   * 设置值，值的类型取决于编辑器编辑的数据
   * @param {String|Object} value 编辑器显示的值
   * @param {Boolean} [hideError=true] 设置值时是否隐藏错误
   */
  setValue : function(value,hideError){
    var _self = this,
      innerControl = _self.getInnerControl();
    _self.set(&#39;editValue&#39;,value);
    _self.clearControlValue();
    innerControl.set(_self.get(&#39;innerValueField&#39;),value);
    if(!value){//编辑的值等于空，则可能不会触发验证
      _self.valid();
    }
    if(hideError){
      _self.clearErrors();
    }
  },
<span id='BUI-Editor-Mixin-method-getValue'>  /**
</span>   * 获取编辑器的值
   * @return {String|Object} 编辑器的值
   */
  getValue :function(){
    var _self = this,
      innerControl = _self.getInnerControl();
    return innerControl.get(_self.get(&#39;innerValueField&#39;));
  },
<span id='BUI-Editor-Mixin-method-isValid'>  /**
</span>   * 编辑的内容是否通过验证
   * @return {Boolean} 是否通过验证
   */
  isValid : function(){
    var _self = this,
      innerControl = _self.getInnerControl();
    return innerControl.isValid ? innerControl.isValid() : true;
  },
<span id='BUI-Editor-Mixin-method-valid'>  /**
</span>   * 验证内容是否通过验证
   */
  valid : function(){
    var _self = this,
      innerControl = _self.getInnerControl();
    innerControl.valid &amp;&amp; innerControl.valid();
  },
<span id='BUI-Editor-Mixin-method-getErrors'>  /**
</span>   * 获取错误信息
   * @return {Array} 错误信息
   */
  getErrors : function(){
     var _self = this,
      innerControl = _self.getInnerControl();
    return innerControl.getErrors ? innerControl.getErrors() : [];
  },
<span id='BUI-Editor-Mixin-method-isChange'>  /**
</span>   * 编辑的内容是否发生改变
   * @return {Boolean}
   */
  isChange : function(){
    var _self = this,
      editValue = _self.get(&#39;editValue&#39;),
      value = _self.getValue();
    return editValue !== value;
  },
<span id='BUI-Editor-Mixin-method-clearValue'>  /**
</span>   * 清除编辑的值
   */
  clearValue : function(){
    this.clearControlValue();
    this.clearErrors();
  },
<span id='BUI-Editor-Mixin-method-clearControlValue'>  /**
</span>   * 清除编辑的控件的值
   * @protected
   * @template
   */
  clearControlValue : function(){
    var _self = this,
      innerControl = _self.getInnerControl();
    innerControl.set(_self.get(&#39;innerValueField&#39;),_self.get(&#39;emptyValue&#39;));
  },
<span id='BUI-Editor-Mixin-method-clearErrors'>  /**
</span>   * 清除错误
   */
  clearErrors : function(){
    var _self = this,
      innerControl = _self.getInnerControl();
    innerControl.clearErrors();
  },
<span id='BUI-Editor-Mixin-method-getSourceValue'>  /**
</span>   * @protected
   * @template
   * 获取编辑的源数据
   */
  getSourceValue : function(){

  },
<span id='BUI-Editor-Mixin-method-updateSource'>  /**
</span>   * @protected
   * @template
   * 更新编辑的源数据
   */
  updateSource : function(){

  },
<span id='BUI-Editor-Mixin-method-handleNavEsc'>  /**
</span>   * @protected
   * @override
   * 处理esc键
   */
  handleNavEsc : function(){
    this.cancel();
  },
<span id='BUI-Editor-Mixin-method-handleNavEnter'>  /**
</span>   * @protected
   * @override
   * 处理enter键
   */
  handleNavEnter : function(ev){
    var sender = ev.target;
    if(sender.tagName === &#39;TEXTAREA&#39;){ //文本输入框，不确定隐藏
      return;
    }
    if(sender.tagName === &#39;BUTTON&#39;){
      $(sender).trigger(&#39;click&#39;);
    }
    this.accept();
  },
<span id='BUI-Editor-Mixin-method-focus'>  /**
</span>   * 设置获取焦点
   */
  focus : function(){
    var _self = this,
      innerControl = _self.getInnerControl();
    innerControl.focus &amp;&amp; innerControl.focus()
  },
<span id='BUI-Editor-Mixin-method-accept'>  /**
</span>   * 接受编辑器的编辑结果
   * @return {Boolean} 是否成功接受编辑
   */
  accept : function(){
    var _self = this,
      value;
    _self.valid();
    if(!_self.isValid()){
      return false;
    }
    value = _self.getValue();

    if(_self.get(&#39;autoUpdate&#39;)){
      _self.updateSource(value);
    }
    if(_self.fire(&#39;beforeaccept&#39;,{value :value}) == false){
      return;
    }
    _self.fire(&#39;accept&#39;,{value :value,editValue : _self.get(&#39;editValue&#39;)});/**/
    _self.hide();
    return true;
  },
<span id='BUI-Editor-Mixin-method-cancel'>  /**
</span>   * 取消编辑
   */
  cancel : function(){
    this.fire(&#39;cancel&#39;);
    this.clearValue();
    this.close();
  }
};

module.exports = Mixin;
</pre>
</body>
</html>
